from bs4 import BeautifulSoup
import requests
import time
import datetime

#反复调用
def get_url(url, params=None, proxies=None):
    fail_time = 0
    cotinue_flag = True
    while (fail_time<=3&cotinue_flag==True):
        try:
            rsp = requests.get(url, params=params, proxies=proxies)
            rsp.raise_for_status()
            cotinue_flag = False
        except Exception as e:
            print(e)
            cotinue_flag=True
            fail_time+=1
            print("睡眠5秒")
            time.sleep(5)
    return rsp.text

#获取基金每日净值数据
#http://fund.eastmoney.com/f10/F10DataApi.aspx?type=lsjz&code=005589&page=1&per=20
#2019-01-02更新，改接口一次最多获取20条记录
def get_fund_data(code, start='', end=''):
    url = 'http://fund.eastmoney.com/f10/F10DataApi.aspx'
    records = []
    continueFlag = True
    page = 0
    while continueFlag == True:
        page+=1
        params = {'type': 'lsjz', 'code': code, 'page': page, 'per': 20, 'sdate': start, 'edate': end}
        html = get_url(url, params)
        soup = BeautifulSoup(html, 'html.parser')
        tab = soup.findAll('tbody')[0]
        if len( tab.findAll('tr'))<20:
            continueFlag=False
        for tr in tab.findAll('tr'):
            if tr.findAll('td') and len((tr.findAll('td'))) == 7:
                #读取页面源数据
                record=[]
                record.append(code)  # 基金号
                record.append(str(tr.select('td:nth-of-type(1)')[0].getText().strip()))#净值日期
                record.append(datetime.datetime.fromtimestamp(time.mktime(time.strptime(str(tr.select('td:nth-of-type(1)')[0].getText().strip()), "%Y-%m-%d"))).weekday()+1)#星期
                record.append(str(tr.select('td:nth-of-type(2)')[0].getText().strip()))#单位净值
                record.append(str(tr.select('td:nth-of-type(3)')[0].getText().strip()))#累计净值

                #  -0.56% --> 0.56
                ChangePercent = str(tr.select('td:nth-of-type(4)')[0].getText().strip())  # 日增长率
                if (len(ChangePercent) == 0):
                    ChangePercent = '0.00'
                else:
                    ChangePercent = ChangePercent[:len(ChangePercent) - 1]
                record.append(ChangePercent)

                record.append(str(tr.select('td:nth-of-type(5)')[0].getText().strip()))#申购状态
                record.append(str(tr.select('td:nth-of-type(6)')[0].getText().strip()))#赎回状态

                # 对源数据进行处理

                #分红配送标志
                #record['convert_drifts'] = str(tr.select('td:nth-of-type(7)')[0].getText().strip())  # 分红配送
                convert_drifts = str(tr.select('td:nth-of-type(7)')[0].getText().strip())  # 分红配送
                if (len(convert_drifts) == 0):
                    Share_flag = 0
                else:
                   #convert_drifts = convert_drifts[8:len(convert_drifts) - 1] #每份基金份额折算1.494202845份-->1.494202845
                    Share_flag = 1
                record.append(Share_flag)
                record.append(convert_drifts)
                records.append(record)
    return records